
# Avoid scanning for build directories
MAKEOBJDIRPREFIX=

# Define all files that are installed on backup server
DISTDIRS      = RB .rb
PROGRAMS      = rb_agent rb_cpanel rb_dump rb_fixinfo rb_restore rb_rotate
PROGRAMSRCS   = rb_agent.in rb_cpanel rb_dump rb_fixinfo rb_restore rb_rotate
SAMPLECONFIGS = .rb/cpanel
MODULES       = RB/Archive.pm RB/Dir.pm RB/Log.pm RB/Profile.pm \
		RB/Remote.pm RB/RotateScheme.pm RB/RotateScheme/Daily.pm \
		RB/RotateScheme/Monthly.pm RB/RotateScheme/Weekly.pm \
		RB/Transfer.pm RB/Util.pm
TEXTS         = CHANGES HACK LICENSE README STYLE UPGRADE
MAN           = rb.7 rb.5 \
                rb_agent.1 rb_cpanel.1 rb_dump.1 rb_fixinfo.1 rb_restore.1 \
                rb_rotate.1
AGENT_USE_MODS= RB/Transfer.pm
MANBUILD      = ${MAN:=.man}
ALLSOURCES    = ${MODULES} ${SAMPLECONFIGS} ${PROGRAMSRCS} ${TEXTS} ${MAN}
BACKUPDIST    = ${MODULES} ${SAMPLECONFIGS} ${PROGRAMS} ${TEXTS} ${MAN}
REMOTEDIST    = ${MODULES} ${SAMPLECONFIGS} ${PROGRAMS} ${TEXTS} ${MANBUILD}

INSTALL_CURUSER           != id -nu
INSTALL_PREFIX            ?= /home/${INSTALL_REMOTE_USER}
INSTALL_REMOTE_USER       ?= ${INSTALL_CURUSER}
INSTALL_REMOTE_TEMP_FILE  ?= /tmp/rbnew.tgz
INSTALL_REMOTE_HOSTS      ?= backup2.agava.net. \
                             backup5.agava.net. \
                             backup7.agava.net.

BINMODE ?= 550
MODMODE ?= 440
CONFMODE ?= 640
DOCMODE ?= 444

REGRESS_TEST              ?= dump rotate show
REGRESS_PROFILE           ?= ${.CURDIR}/.rb/test
REGRESS_HOSTS             ?= 172.27.3.1
REGRESS_DUMP_FLAGS        ?= -i -N -n -v
REGRESS_ROTATE_FLAGS      ?= -n -v
REGRESS_SHOW_FLAGS        ?= -v

MANPROG = nroff -Tascii -mandoc


all: rb_agent manbuild

rb_agent: rb_agent.in ${AGENT_USE_MODS}
	awk 'NR == 1,    $$0 !~ /^#/ { print; }' rb_agent.in  >$@
	awk '/^package/, $$0 == "1;" \
		{ if ($$0 != "1;" && $$0 !~ /^[\s]*use RB::/) { print; } }' \
		${AGENT_USE_MODS} >>$@
	awk '/^package/, 1 == 0      { print; }' rb_agent.in >>$@
	chmod a+x $@

install-cpanel: .MAKE .PHONY
	cd ${.CURDIR}/cpanel && ${.MAKE} install

install: .PHONY
	@echo "===> Installing files"
	install -d ${DISTDIRS:S@^@${INSTALL_PREFIX}/@}
.	for FILE in ${MODULES}
		install -m ${MODMODE} ${FILE} ${INSTALL_PREFIX}/${FILE}
.	endfor
.	for FILE in ${PROGRAMS}
		install -m ${BINMODE} ${FILE} ${INSTALL_PREFIX}/${FILE}
.	endfor
.	for FILE in ${SAMPLECONFIGS}
		install -m ${CONFMODE} ${FILE} ${INSTALL_PREFIX}/${FILE}
.	endfor
.	for FILE in ${TEXTS}
		install -m ${DOCMODE} ${FILE} ${INSTALL_PREFIX}/${FILE}
.	endfor

install-cron: .PHONY
	@echo "===> Installing sample crontab line"
	if ! crontab -l | fgrep -- "/rb_dump"; then \
		(crontab -l; echo "# Sample RB dumping line"; \
		    echo "#18 0 * * * ${INSTALL_PREFIX}/rb_dump cpanel cpXXX.agava.net") | \
		    crontab -; \
	fi

install-all: install install-cron

deinstall:
	rm -f ${BACKUPDIST:S@^@${INSTALL_PREFIX}/@}
	rmdir -p ${INSTALL_PREFIX}/RB ${INSTALL_PREFIX}/.rb

clean:
	rm -f rb.tgz rb_agent
	find . -name '*~' -or -name '*.diff' -or -name '*.orig' \
	    -or -name '*.rej' -or -name '*.man' | \
	    xargs -tr rm --

rb.tgz: ${REMOTEDIST}
	pax -wzf ${@} -- ${REMOTEDIST}

remote-install: rb.tgz .IGNORE
.for HOST in ${INSTALL_REMOTE_HOSTS}
	@echo "==> You can be asked for password up to three times, be ready..."

# Do not fail: errors will be shown, and we want to try all servers
	scp rb.tgz ${HOST}:${INSTALL_REMOTE_TEMP_FILE} && \
	    ssh -tt ${HOST} sudo -u dump \
		'sh -c "cd /home/dump && pax -rzf ${INSTALL_REMOTE_TEMP_FILE} \
		&& chmod a+x ${PROGRAMS}"'
.endfor

# Man pages building
.for _P in ${MAN}
${_P:=.man}: ${_P}
	${MANPROG} ${_P} > $@
.endfor
manbuild: ${MANBUILD}

regress:
	cd "${.CURDIR}/regress" && ${.MAKE} $@

stats: ${MANBUILD}
	@cat ${BACKUPDIST:Nrb_agent} rb_agent.in | \
		wc | awk '{print "Total code: " $$1 " lines, " $$3 " bytes"; }'
	@cat ${PROGRAMSRCS} ${MODULES} | \
		sed -e 's/^[[:space:]]*//' -e '/^\#/d' | \
		wc | awk '{print "  Raw code: " $$1 " lines, " $$3 " bytes"; }'
	@cat ${TEXTS} ${MANBUILD} | \
		wc | awk '{print "Total docs: " $$1 " lines, " $$3 " bytes"; }'
	@cat ${TEXTS} ${MANBUILD} | sed -e '/^[[:space:]]*$$/d' | \
		wc | awk '{print "  Raw docs: " $$1 " lines, " $$3 " bytes"; }'
	@echo -n "Other stats:"
	@printf " %d source files," `echo ${ALLSOURCES} | wc -w`
	@printf " %d packages," `${.MAKE} egrep what=^package | fgrep -v "package main" | wc -l`
	@printf " %d subroutines\n" `${.MAKE} egrep what=^sub | wc -l`

.for _P in fgrep egrep grep
${_P}:
.if !defined(what) || empty(what)
	@echo "usage: make what=pattern" >&2
.else
	-@${_P} -- "${what}" ${PROGRAMSRCS} ${MODULES}
.endif
.endfor

